<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Daftar_keluarga extends CI_Controller {
	
	function __construct(){		
		parent::__construct();	
		//$this->load->library('Excel_generator');
		//$this->output->enable_profiler(true);
		
		$this->load->model('mdl_daftar_keluarga');
	}
	
	function index(){
		$data['page_title']='Daftar Keluarga';
		$data['page_icon']='icon-group';
		$data['page_content']='daftar_keluarga';
		
		$this->load->view('layout/master',$data);
	}
	
	
	function grid(){
		$data = $this->mdl_daftar_keluarga->getdata();
		echo $this->mdl_daftar_keluarga->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		// untuk grid
		$data['anggota_keluarga'] = json_encode(array('total'=>0, 'rows'=>array()));

		$data['id_keluarga'] = '';
		$data['alamat'] = '';
		$data['kepala_keluarga'] = '';
		$data['dusun'] = '';
		$data['rt'] = '';
		$data['rw'] = '';
		$data['ekonomi'] = '';		
		$data['thn_daftar'] = '';
		$this->load->view('daftar_keluarga_form', $data);
	}
	
	function save($aksi="add"){
		# init
		$status = "";
		$result = false;
		//$data['pesan_error'] = '';
		$data = '';
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}


		// $data['list_anggota_keluarga'] = json_decode($data['anggota_keluarga']);
		// unset($data['anggota_keluarga']);
		
		# rules validasi form
		$this->form_validation->set_rules("id_keluarga", 'No. KK', 'trim|required|xss_clean');
		$this->form_validation->set_rules("kepala_keluarga", 'Kepala Keluarga', 'trim|required|xss_clean');
		$this->form_validation->set_rules("alamat", 'Alamat', 'trim|required|xss_clean');
		$this->form_validation->set_rules("dusun", 'Dusun', 'trim|required|xss_clean');
		$this->form_validation->set_rules("rt", 'RT', 'trim|required|xss_clean');
		$this->form_validation->set_rules("rw", 'RW', 'trim|required|xss_clean');
		$this->form_validation->set_rules("ekonomi", 'Ekonomi', 'trim|required|xss_clean');
		$this->form_validation->set_rules("thn_daftar", 'Tahun Terdaftar', 'trim|required|xss_clean');		
		
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			//$data["pesan_error"] .= trim(form_error('kd_biaya',' ',' '))==''?'':form_error('kd_biaya',' ',' ').'<br>';
			$data["pesan_error"] .= trim(validation_errors(' ',' '))==''?'':validation_errors(' ',' ');
		}else{
			if($aksi == 'add'){
				$result = $this->mdl_daftar_keluarga->InsertOnDb($data);
			}else{
				$result = $this->mdl_daftar_keluarga->UpdateOnDb($data);
			}
		}
		
		if($result){
			echo json_encode(array('success'=>true));
		}else{
			echo json_encode(array('success'=>false, 'msg'=>$data['pesan_error']));
		}
		
	}

	function detail($id_keluarga){
		$r = $this->mdl_daftar_keluarga->getDataEdit($id_keluarga);

		$data['id_keluarga'] = $r->row()->id_keluarga;
		$data['nama_kk'] = $r->row()->nama_kk;
		$data['kepala_keluarga'] = $r->row()->kepala_keluarga;
		$data['alamat'] = $r->row()->alamat;
		$data['dusun'] = $r->row()->dusun;
		$data['rt'] = $r->row()->rt;
		$data['rw'] = $r->row()->rw;
		$data['ekonomi'] = $r->row()->ekonomi;
		$data['thn_daftar'] = $r->row()->thn_daftar;
		$data['anggota_keluarga'] = $this->mdl_daftar_keluarga->getDataAnggota($id_keluarga);

		$this->load->view('daftar_keluarga_detail', $data);
	}

	function edit($id_keluarga){
		$r = $this->mdl_daftar_keluarga->getDataEdit($id_keluarga);

		$data['id_keluarga'] = $r->row()->id_keluarga;
		$data['nama_kk'] = $r->row()->nama_kk;
		$data['kepala_keluarga'] = $r->row()->kepala_keluarga;
		$data['alamat'] = $r->row()->alamat;
		$data['dusun'] = $r->row()->dusun;
		$data['rt'] = $r->row()->rt;
		$data['rw'] = $r->row()->rw;
		$data['ekonomi'] = $r->row()->ekonomi;
		$data['thn_daftar'] = $r->row()->thn_daftar;
		$data['anggota_keluarga'] = json_encode($this->mdl_daftar_keluarga->getDataAnggota($id_keluarga)->result());
		// $data['no_ktp'] = $r->row()->no_ktp;

		$this->load->view('daftar_keluarga_form', $data);
	}

	function get_nama_by_ktp(){
		$ktp = $this->input->post('no_ktp');
		$data = $this->mdl_daftar_keluarga->getDataByktp($ktp)->row();

		echo $data->nama;
	}

	function list_penduduk(){
		$data = $this->mdl_daftar_keluarga->getListPenduduk()->result();

		echo json_encode($data);
	}

	function exportExcel(){
		//load our new PHPExcel library
		$this->load->library('excel');
		
		//activate worksheet number 1
		$this->excel->setActiveSheetIndex(0);
		
		//name the worksheet
		$this->excel->getActiveSheet()->setTitle('Laporan Data Keluarga');

		# Init Header
		//$this->excel->getActiveSheet()->getColumnDimension('A:AB')->setAutoSize(true);
		//$this->excel->getActiveSheet()->getStyle('A1:AB2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$this->excel->getActiveSheet()->setCellValue('A1', 'Laporan Data Keluarga')
									  ->setCellValue('A3', 'No KK')
									  ->setCellValue('B3', 'Kepala Keluarga')
									  ->setCellValue('C3', 'Alamat')
									  ->setCellValue('D3', 'Dusun')
									  ->setCellValue('E3', 'RT')
									  ->setCellValue('F3', 'RW')
									  ->setCellValue('G3', 'Anggota Keluarga');

		#iterasi
		$curpos = 4;
		$kk = $this->mdl_daftar_keluarga->getDataKK();
		foreach ($kk->result() as $row) {
			# code...
			$this->excel->getActiveSheet()->setCellValueExplicitByColumnAndRow(0, $curpos, $row->id_keluarga, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(1, $curpos, $row->nama, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(2, $curpos, $row->alamat, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(3, $curpos, $row->nama_desa, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(4, $curpos, $row->rt, PHPExcel_Cell_DataType::TYPE_STRING)
										  ->setCellValueExplicitByColumnAndRow(5, $curpos, $row->rw, PHPExcel_Cell_DataType::TYPE_STRING);
			
			//iterasi Anggota
			$anggota = $this->mdl_daftar_keluarga->getAnggotaByKK($row->id_keluarga, $row->kepala_keluarga);
			if ($anggota->num_rows() > 0) {
				# code...
				foreach ($anggota->result() as $ang) {
					# code...
					$this->excel->getActiveSheet()->setCellValue('G'.$curpos, $ang->nama);
					$curpos++;
				}
			}else{
				$curpos++;
			}

									  	  
		}
		#style
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('A3:G3')->getFont()->setBold(true);
		$this->excel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);
		$this->excel->getActiveSheet()->mergeCells('A1:G1');
		$this->excel->getActiveSheet()->getStyle('A1:G1')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER)->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		# init Column Color
		$this->excel->getActiveSheet()->getStyle('A3:G3')->getFill()
					->setFillType(PHPExcel_Style_Fill::FILL_SOLID)
					->getStartColor()->setRGB('34efbd');

		# set border
		$styleArray = array(
			'borders' => array(
				'outline' => array(
					'style' => PHPExcel_Style_Border::BORDER_THIN,
					'color' => array('rgb' => '000000'),
				),
				'inside' => array(
					'style' => PHPExcel_Style_Border::BORDER_HAIR	,
					'color' => array('rgb' => '000000'),
				),
			),
		);
		$this->excel->getActiveSheet()->getStyle('A3:G'.($curpos-1))->applyFromArray($styleArray);

		# begin download
		$filename= 'Laporan.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
					 
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
		//force user to download the Excel file without writing it to server's HD
		$objWriter->save('php://output');

	}


}